package com.zch.jh.web.rest;

import com.zch.jh.JhbbApp;
import com.zch.jh.domain.ReceiveAddr;
import com.zch.jh.repository.ReceiveAddrRepository;
import com.zch.jh.service.ReceiveAddrService;
import com.zch.jh.service.dto.ReceiveAddrDTO;
import com.zch.jh.service.mapper.ReceiveAddrMapper;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Integration tests for the {@link ReceiveAddrResource} REST controller.
 */
@SpringBootTest(classes = JhbbApp.class)
@AutoConfigureMockMvc
@WithMockUser
public class ReceiveAddrResourceIT {

    private static final String DEFAULT_RECEIVE_NAME = "AAAAAAAAAA";
    private static final String UPDATED_RECEIVE_NAME = "BBBBBBBBBB";

    private static final String DEFAULT_RECEIVE_PHONE = "AAAAAAAAAA";
    private static final String UPDATED_RECEIVE_PHONE = "BBBBBBBBBB";

    private static final String DEFAULT_RECEIVE_ADDR = "AAAAAAAAAA";
    private static final String UPDATED_RECEIVE_ADDR = "BBBBBBBBBB";

    @Autowired
    private ReceiveAddrRepository receiveAddrRepository;

    @Autowired
    private ReceiveAddrMapper receiveAddrMapper;

    @Autowired
    private ReceiveAddrService receiveAddrService;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restReceiveAddrMockMvc;

    private ReceiveAddr receiveAddr;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static ReceiveAddr createEntity(EntityManager em) {
        ReceiveAddr receiveAddr = new ReceiveAddr()
            .receiveName(DEFAULT_RECEIVE_NAME)
            .receivePhone(DEFAULT_RECEIVE_PHONE)
            .receiveAddr(DEFAULT_RECEIVE_ADDR);
        return receiveAddr;
    }
    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static ReceiveAddr createUpdatedEntity(EntityManager em) {
        ReceiveAddr receiveAddr = new ReceiveAddr()
            .receiveName(UPDATED_RECEIVE_NAME)
            .receivePhone(UPDATED_RECEIVE_PHONE)
            .receiveAddr(UPDATED_RECEIVE_ADDR);
        return receiveAddr;
    }

    @BeforeEach
    public void initTest() {
        receiveAddr = createEntity(em);
    }

    @Test
    @Transactional
    public void createReceiveAddr() throws Exception {
        int databaseSizeBeforeCreate = receiveAddrRepository.findAll().size();
        // Create the ReceiveAddr
        ReceiveAddrDTO receiveAddrDTO = receiveAddrMapper.toDto(receiveAddr);
        restReceiveAddrMockMvc.perform(post("/api/receive-addrs")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(receiveAddrDTO)))
            .andExpect(status().isCreated());

        // Validate the ReceiveAddr in the database
        List<ReceiveAddr> receiveAddrList = receiveAddrRepository.findAll();
        assertThat(receiveAddrList).hasSize(databaseSizeBeforeCreate + 1);
        ReceiveAddr testReceiveAddr = receiveAddrList.get(receiveAddrList.size() - 1);
        assertThat(testReceiveAddr.getReceiveName()).isEqualTo(DEFAULT_RECEIVE_NAME);
        assertThat(testReceiveAddr.getReceivePhone()).isEqualTo(DEFAULT_RECEIVE_PHONE);
        assertThat(testReceiveAddr.getReceiveAddr()).isEqualTo(DEFAULT_RECEIVE_ADDR);
    }

    @Test
    @Transactional
    public void createReceiveAddrWithExistingId() throws Exception {
        int databaseSizeBeforeCreate = receiveAddrRepository.findAll().size();

        // Create the ReceiveAddr with an existing ID
        receiveAddr.setId(1L);
        ReceiveAddrDTO receiveAddrDTO = receiveAddrMapper.toDto(receiveAddr);

        // An entity with an existing ID cannot be created, so this API call must fail
        restReceiveAddrMockMvc.perform(post("/api/receive-addrs")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(receiveAddrDTO)))
            .andExpect(status().isBadRequest());

        // Validate the ReceiveAddr in the database
        List<ReceiveAddr> receiveAddrList = receiveAddrRepository.findAll();
        assertThat(receiveAddrList).hasSize(databaseSizeBeforeCreate);
    }


    @Test
    @Transactional
    public void getAllReceiveAddrs() throws Exception {
        // Initialize the database
        receiveAddrRepository.saveAndFlush(receiveAddr);

        // Get all the receiveAddrList
        restReceiveAddrMockMvc.perform(get("/api/receive-addrs?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(receiveAddr.getId().intValue())))
            .andExpect(jsonPath("$.[*].receiveName").value(hasItem(DEFAULT_RECEIVE_NAME)))
            .andExpect(jsonPath("$.[*].receivePhone").value(hasItem(DEFAULT_RECEIVE_PHONE)))
            .andExpect(jsonPath("$.[*].receiveAddr").value(hasItem(DEFAULT_RECEIVE_ADDR)));
    }
    
    @Test
    @Transactional
    public void getReceiveAddr() throws Exception {
        // Initialize the database
        receiveAddrRepository.saveAndFlush(receiveAddr);

        // Get the receiveAddr
        restReceiveAddrMockMvc.perform(get("/api/receive-addrs/{id}", receiveAddr.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(receiveAddr.getId().intValue()))
            .andExpect(jsonPath("$.receiveName").value(DEFAULT_RECEIVE_NAME))
            .andExpect(jsonPath("$.receivePhone").value(DEFAULT_RECEIVE_PHONE))
            .andExpect(jsonPath("$.receiveAddr").value(DEFAULT_RECEIVE_ADDR));
    }
    @Test
    @Transactional
    public void getNonExistingReceiveAddr() throws Exception {
        // Get the receiveAddr
        restReceiveAddrMockMvc.perform(get("/api/receive-addrs/{id}", Long.MAX_VALUE))
            .andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    public void updateReceiveAddr() throws Exception {
        // Initialize the database
        receiveAddrRepository.saveAndFlush(receiveAddr);

        int databaseSizeBeforeUpdate = receiveAddrRepository.findAll().size();

        // Update the receiveAddr
        ReceiveAddr updatedReceiveAddr = receiveAddrRepository.findById(receiveAddr.getId()).get();
        // Disconnect from session so that the updates on updatedReceiveAddr are not directly saved in db
        em.detach(updatedReceiveAddr);
        updatedReceiveAddr
            .receiveName(UPDATED_RECEIVE_NAME)
            .receivePhone(UPDATED_RECEIVE_PHONE)
            .receiveAddr(UPDATED_RECEIVE_ADDR);
        ReceiveAddrDTO receiveAddrDTO = receiveAddrMapper.toDto(updatedReceiveAddr);

        restReceiveAddrMockMvc.perform(put("/api/receive-addrs")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(receiveAddrDTO)))
            .andExpect(status().isOk());

        // Validate the ReceiveAddr in the database
        List<ReceiveAddr> receiveAddrList = receiveAddrRepository.findAll();
        assertThat(receiveAddrList).hasSize(databaseSizeBeforeUpdate);
        ReceiveAddr testReceiveAddr = receiveAddrList.get(receiveAddrList.size() - 1);
        assertThat(testReceiveAddr.getReceiveName()).isEqualTo(UPDATED_RECEIVE_NAME);
        assertThat(testReceiveAddr.getReceivePhone()).isEqualTo(UPDATED_RECEIVE_PHONE);
        assertThat(testReceiveAddr.getReceiveAddr()).isEqualTo(UPDATED_RECEIVE_ADDR);
    }

    @Test
    @Transactional
    public void updateNonExistingReceiveAddr() throws Exception {
        int databaseSizeBeforeUpdate = receiveAddrRepository.findAll().size();

        // Create the ReceiveAddr
        ReceiveAddrDTO receiveAddrDTO = receiveAddrMapper.toDto(receiveAddr);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restReceiveAddrMockMvc.perform(put("/api/receive-addrs")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(receiveAddrDTO)))
            .andExpect(status().isBadRequest());

        // Validate the ReceiveAddr in the database
        List<ReceiveAddr> receiveAddrList = receiveAddrRepository.findAll();
        assertThat(receiveAddrList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    public void deleteReceiveAddr() throws Exception {
        // Initialize the database
        receiveAddrRepository.saveAndFlush(receiveAddr);

        int databaseSizeBeforeDelete = receiveAddrRepository.findAll().size();

        // Delete the receiveAddr
        restReceiveAddrMockMvc.perform(delete("/api/receive-addrs/{id}", receiveAddr.getId())
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<ReceiveAddr> receiveAddrList = receiveAddrRepository.findAll();
        assertThat(receiveAddrList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
